package com.wude.guild;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mc.developmentkit.utils.MCUtils;
import com.umeng.analytics.MobclickAgent;
import com.wude.guild.fragment.GameDetIntroductionFragment;
import com.wude.guild.servercore.mints.MintsBaseActivity;

import org.xutils.x;

import java.util.List;

public class ScreenshotsActivity extends MintsBaseActivity {
    private ViewPager viewPager;
    private MyAdapter adapter;
    private LinearLayout home_circle;
    private List<String> topTus;
    private int mPreviousPos;       //上一个页面

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screenshots);

        initView();
    }

    private void initData() {

        for (int i = 0; i < topTus.size(); i++) {
            ImageView imageView = new ImageView(x.app());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            if (i != 0) {
                layoutParams.leftMargin = 15;
                imageView.setLayoutParams(layoutParams);
                imageView.setEnabled(false);
            }
            imageView.setBackgroundResource(R.drawable.indicater);   //被选中圆点变大
            home_circle.addView(imageView);
        }
    }

    private void initView() {
        viewPager = findViewById(R.id.viewPager);
        home_circle = findViewById(R.id.home_circle);
        initViewPagerNum();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initViewPagerNum() {
        List<String> screenshot = GameDetIntroductionFragment.appInfo.jietu;
        topTus = screenshot;
        initData();
        adapter = new MyAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setOnPageChangeListener(onPageChangeListener);
    }


    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            int pos = arg0 % topTus.size();
            home_circle.getChildAt(pos).setEnabled(true);//将选中的页面的圆点设置为红色
            home_circle.getChildAt(mPreviousPos).setEnabled(false);//上一个页面的圆点设置为灰色
            mPreviousPos = arg0;  //更新上一个页面的位置
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };

    class MyAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return topTus.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            MCUtils.fillImage(imageView, topTus.get(position));
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
